﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace QuanLyThuNgan
{
    public partial class Frm_ThanhToanXV : DevExpress.XtraEditors.XtraForm
    {
        public Frm_ThanhToanXV()
        {
            InitializeComponent();
        }
        private int iloai = 0;
        DonVi_Khac dv_khac = new DonVi_Khac();
        ThuDTLCTL thuDTLCtl = new ThuDTLCTL();
        ThuDTLDTO thuDTLDto = new ThuDTLDTO();
        CaThuNganCTL caThuNganCtl = new CaThuNganCTL();
        CaThuNganDTO caThuNganDto = new CaThuNganDTO();
        DataTable dtLoadDSPhieu = new DataTable();
        BenhNhanInfo bnInfo = new BenhNhanInfo();
        
        

        private void Frm_ThanhToanXV_Load(object sender, EventArgs e)
        {
            LoadKhoa();
            m_ReadOnlyThanhToanXV();
            
            dtkt = caThuNganCtl.m_SelectKiemTraNguoiDungTTXV(iIDNguoiDung);
            iloai = Uit.it_Parse.ToInteger(dtkt.Rows[0]["loai"].ToString());
            if (iloai >= 1)
            {
                btnLuuXV.Enabled = true;
                btnINXV.Enabled = true;
                btnTimKiem.Enabled = true;
                btnBoQua.Enabled = true;
            }
            else
            {
                btnLuuXV.Enabled = false;
                btnINXV.Enabled = false;
                btnTimKiem.Enabled = false;
                btnBoQua.Enabled = false;
            }
           
        }

        #region "Tab Danh Sach Benh Nhan Xuat Vien"
        private void LoadKhoa()
        {
            DataTable dtDonVi = dv_khac.DMDonVi();
            ClassHelp.m_SearchLookUpEdit(dtDonVi, gdSearchKhoa, "TenDonVi", "MaDonVi");

            txtSoHS.Focus();
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            if (txtHo.Text == "" && txtSoHS.Text == "")
                if (txtSoNhapVien.Text == "" && gdSearchKhoa.Text == "")
                    if (txtNamSinh.Text == "" && txtTen.Text == "")
                        return;

            DataTable dtdsbenhnhan = thuDTLCtl.m_SelectDSBNThanhToanXuatVien(txtSoHS.Text, txtHo.Text, txtTen.Text,
            txtNamSinh.Text == "" ? 0 : Uit.it_Parse.ToInteger(txtNamSinh.Text), txtSoNhapVien.Text,
            gdSearchKhoa.Text == "" ? "" : gdSearchKhoa.EditValue.ToString());
            gdDSBN.DataSource = dtdsbenhnhan;
        }

        private void btnBoQua_Click(object sender, EventArgs e)
        {
            txtHo.Text = "";
            txtSoHS.Text = "";
            txtSoNhapVien.Text = "";
            gdSearchKhoa.EditValue = -1;
            txtNamSinh.Text = "";
            txtTen.Text = "";
        }

        private void gvDSBN_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            //lay gia tri benh nhan------------------------------
            DataRow dr = gvDSBN.GetDataRow(e.FocusedRowHandle);
            if (dr == null)
                return;
            bnInfo.MaSo = dr["SoHS"].ToString();
            bnInfo.Ho = dr["Ho"].ToString();
            bnInfo.Ten = dr["Ten"].ToString();
            bnInfo.NamSinh = Uit.it_Parse.ToInteger(dr["namsinh"].ToString());
            bnInfo.Gioi = dr["GioiTinh"].ToString();
            bnInfo.DiaChi = dr["DiaChi"].ToString();
            bnInfo.Ngaynhapvien = dr["ngaynhapvien"].ToString();
            bnInfo.SoNhapVien = dr["sonhapvien"].ToString();
            bnInfo.Ngayxuatvien = dr["ngayxuatvien"].ToString();
            bnInfo.Traibenh = dr["TenDonVi"].ToString();
            bnInfo.ChanDoanXV = dr["chandoanxv"].ToString();
            bnInfo.Bhytxv = Uit.it_Parse.ToBoolean(dr["bhytxv"].ToString());
            //trong goi------------------------------------
            bnInfo.TenTronGoi = dr["TenTronGoi"].ToString();
            bnInfo.TienTronGoi = Uit.it_Parse.ToInteger(dr["tientrongoi"].ToString());
            bnInfo.NgayBDTronGoi = dr["ngaybdtrongoi"].ToString();
            bnInfo.NgayKTTronGoi = dr["ngaykttrongoi"].ToString();
            //load gia tri len tab xuat vien----------------------------------
            txtSohsXV.Text = bnInfo.MaSo;
            txtHoTenXV.Text = bnInfo.Ho + " " + bnInfo.Ten;
            txtNamSinhXV.Text = bnInfo.NamSinh.ToString();
            txtGioiTinhXV.Text = bnInfo.Gioi;
            txtDiaChiXV.Text = bnInfo.DiaChi;
            txtNgayNhapVienXV.Text = bnInfo.Ngaynhapvien;
            txtSoNhapVienXV.Text = bnInfo.SoNhapVien;
            txtNgayXV.Text = bnInfo.Ngayxuatvien;
            txtTraiBenhXV.Text = bnInfo.Traibenh;
            txtChanDoanXV.Text = bnInfo.ChanDoanXV;
            chkBenhNhanBHYT.Checked = bnInfo.Bhytxv;
            //---------------------------
            txtTenTronGoi.Text = bnInfo.TenTronGoi;
            txtTienTronGoi.Text = bnInfo.TienTronGoi.ToString(); ;
            txtNgayBDTronGoi.Text = bnInfo.NgayBDTronGoi;
            txtNgayKTTronGoi.Text = bnInfo.NgayKTTronGoi;
            if (bnInfo.MaSo != "")
            {
                DataTable dtttxv = thuDTLCtl.m_SelectThanhToanXuatVien(bnInfo.MaSo, bnInfo.SoNhapVien);
                foreach (DataRow drxv in dtttxv.Rows)
                {
                    txtTongChiPhiXV.Text = drxv["ChiPhi"].ToString();
                    txtDongTamUngXV.Text = drxv["TamUng"].ToString();
                    txtBHYTThanhToanXV.Text = drxv["BHYTTT"].ToString();
                    txtThanhToanXV.Text = drxv["ThanhToanXV"].ToString();
                }
            }
            //load danh sach xuat vien----------------------------------
            DataTable dtdsxv = thuDTLCtl.m_DanhSachXuatVien(txtSohsXV.Text, txtSoNhapVienXV.Text);
            gdDanhSachXuatVien.DataSource = dtdsxv;
        }

        #endregion

        void m_ReadOnlyThanhToanXV()
        {
            //ThongTinBN---------------
            txtSohsXV.Properties.ReadOnly = true;
            txtHoTenXV.Properties.ReadOnly = true;
            txtNamSinhXV.Properties.ReadOnly = true;
            txtGioiTinhXV.Properties.ReadOnly = true;
            txtDiaChiXV.Properties.ReadOnly = true;
            txtNgayNhapVienXV.Properties.ReadOnly = true;
            txtSoNhapVienXV.Properties.ReadOnly = true;
            txtNgayXV.Properties.ReadOnly = true;
            txtTraiBenhXV.Properties.ReadOnly = true;
            chkBenhNhanBHYT.Properties.ReadOnly = true;
            txtChanDoanXV.Properties.ReadOnly= true;
            //TronGoi---------------------------------
            txtTenTronGoi.Properties.ReadOnly = true;
            txtTienTronGoi.Properties.ReadOnly = true;
            txtNgayBDTronGoi.Properties.ReadOnly = true;
            txtNgayKTTronGoi.Properties.ReadOnly = true;
            //ThongTinThanhToanXV-----------------------
            txtTongChiPhiXV.Properties.ReadOnly = true;
            txtDongTamUngXV.Properties.ReadOnly = true;
            txtBHYTThanhToanXV.Properties.ReadOnly = true;
            txtThanhToanXV.Properties.ReadOnly = true;
        }

        #region "Tab Thanh Toan Xuat Vien"
        
        int iIDNguoiDung = frmLogin.nd.IDNguoiDung;
        private DataTable dtkt;
        private void btnLuuXV_Click(object sender, EventArgs e)
        {
            
            thuDTLDto.Ssohs = txtSohsXV.Text;
            thuDTLDto.Ssonhapvien = txtSoNhapVienXV.Text;
            thuDTLDto.Sngaynop = Uit.it_Date.CurrentDate("yyyy-MM-dd");
            thuDTLDto.Fsotien = float.Parse(txtThanhToanXV.Text);
            thuDTLDto.Ifeeid = 5;
            dtkt = caThuNganCtl.m_SelectKiemTraNguoiDungTTXV(iIDNguoiDung);
            thuDTLDto.IIDCaThuNgan = Uit.it_Parse.ToInteger(dtkt.Rows[0]["IDCaTHuNgan"].ToString());
            thuDTLDto.Snoivao = dtkt.Rows[0]["MaDonVi"].ToString();
            thuDTLDto.IIDNguoiNhap = iIDNguoiDung;
            iloai = Uit.it_Parse.ToInteger(dtkt.Rows[0]["loai"].ToString());
            if (iloai >= 1)
            {
                thuDTLCtl.m_InsertThanhToanXV(thuDTLDto);
                XtraMessageBox.Show("Insert thành công");        
            }
            else
            {
                XtraMessageBox.Show("Kết quả đã thanh toán", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            btnLuuXV.Enabled = false;
        }

        //thoat-------------------------------------------
        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //in thanh toan xuat vien----------------------------
        private void btnINXV_Click(object sender, EventArgs e)
        {
            rpt_InThanhToanXuatVien rpt = new rpt_InThanhToanXuatVien();
            rpt.sSohs = txtSohsXV.Text;
            rpt.sSoNhapVien = txtSoNhapVienXV.Text;    
            rpt.sHoTen = txtHoTenXV.Text;
            rpt.sGioiTinh = txtGioiTinhXV.Text;
            rpt.sNamSinh = txtNamSinhXV.Text;
            rpt.sDiaChi = txtDiaChiXV.Text;
            rpt.sKhoa = txtTraiBenhXV.Text;
            rpt.sTienVienPhi = txtTongChiPhiXV.Text;
            rpt.sTienTamUng= txtDongTamUngXV.Text;
            rpt.sTienBHYT = txtBHYTThanhToanXV.Text;
            rpt.sTienHoanTra = txtThanhToanXV.Text;
            rpt.ShowPreview();
            
        }

        #endregion

        

        

    }
}